package com.example.module_home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView.OnItemSelectedListener
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_common.view.entity.ServiceEntity
import com.example.lib_image.glide.glide.GlideImgLoader
import com.example.module_home.databinding.ItemAreahouseBinding
import com.example.module_home.databinding.ItemHotServiceBinding

class HotServiceAdapter(private val data:List<ServiceEntity>):RecyclerView.Adapter<HotServiceAdapter.HotItemViewHolder>() {

    private var itemOnClickListener:ItemOnClickListener?=null

    fun setItemOnCilkListener(listener: ItemOnClickListener){
        this.itemOnClickListener=listener
    }

    inner class HotItemViewHolder(val itemDataBinding:ItemHotServiceBinding):RecyclerView.ViewHolder(itemDataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HotItemViewHolder {
        return HotItemViewHolder(ItemHotServiceBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int {
       return data.size
    }

    override fun onBindViewHolder(holder: HotItemViewHolder, position: Int) {
        val entity=data[position]
        holder.itemDataBinding.hotServiceName.text=entity.name
        GlideImgLoader.loadResImage(holder.itemView,entity.resId,holder.itemDataBinding.hotServiceImg)

        //点击事件
        holder.itemDataBinding.root.setOnClickListener {
            itemOnClickListener?.onClick(it,position)
        }
    }

    /**
     *  自定义点击事件接口
     */
    interface ItemOnClickListener{
        fun onClick(v: View, position: Int)
    }

}